#ifndef THREADWORK_H
#define THREADWORK_H

#include <ConcurrentPipeline.h>
#include <qobject.h>
#include <qthread.h>

///线程耗时读取

class ThreadWork : public QObject {
    Q_OBJECT
public:
    ThreadWork(ConcurrentPipeline<int>* pipeline, QObject* parent = nullptr)
        : QObject(parent), m_pipeline(pipeline) {}
public slots:
    void start() {
        while (!m_stopped) {
            QList<int> batch = m_pipeline->popBatch(50, 100); // 超时100ms
            if (!batch.isEmpty()) {
                emit newEcgData(batch);
            }
            QThread::msleep(1); // 避免线程完全占用CPU
        }
    }
    void stop() { m_stopped = true; }

signals:
    void newEcgData(const QList<int>& batch);

private:
    ConcurrentPipeline<int>* m_pipeline;
    bool m_stopped = false;
};


#endif // THREADWORK_H

// demo
// ThreadWork* reader = new ThreadWork(&m_data);
// QThread* thread = new QThread;
// reader->moveToThread(thread);

// connect(thread, &QThread::started, reader, &ThreadWork::start);
// connect(reader, &ThreadWork::newEcgData, ecgWidget, &EcgWaveWidget::appendData);
// connect(thread, &QThread::finished, reader, &QObject::deleteLater);

// thread->start();
